package com.gitee.zhanyao.itext.util;

import com.itextpdf.kernel.geom.AffineTransform;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * @author zhanyao
 */
@Slf4j
public class PdfConvertUtil {

    private PdfConvertUtil() {
    }

    public static void convertToA4(String source, String target) {
        try (PdfReader pdfReader = new PdfReader(source);
             PdfDocument pdfReaderDocument = new PdfDocument(pdfReader);
             PdfWriter pdfWriter = new PdfWriter(target);
             PdfDocument pdfWriterDocument = new PdfDocument(pdfWriter)) {
            for (int pageNum = 1; pageNum <= pdfReaderDocument.getNumberOfPages(); pageNum++) {
                PdfPage origPage = pdfReaderDocument.getPage(pageNum);
                Rectangle origRectangle = origPage.getPageSizeWithRotation();
                // 标准 A4 为 595 x 842，存在特殊情况：如 Rectangle: 595.2 x 841.92，所以四舍五入
                int origWidth = Math.round(origRectangle.getWidth());
                int origHeight = Math.round(origRectangle.getHeight());
                // 页面旋转角度：90度纸张方向向左；180度纸张方向向下；270度纸张方向向右
                int rotation = origPage.getRotation();
                // 创建A4页面
                PdfPage newPage = pdfWriterDocument.addNewPage(PageSize.A4);
                float newWidth = newPage.getPageSize().getWidth();
                float newHeight = newPage.getPageSize().getHeight();
                PdfCanvas canvas = new PdfCanvas(newPage);
                // 缩放
                AffineTransform scaleMatrix = AffineTransform.getScaleInstance(
                        newWidth / origWidth, newHeight / origHeight);
                // 旋转：基于左下角顺时针旋转
                AffineTransform rotateMatrix = AffineTransform.getRotateInstance(-rotation * Math.PI / 180);
                // 平移
                double translateX = 0;
                double translateY = 0;
                if (rotation == 90) {
                    translateY = newHeight;
                } else if (rotation == 180) {
                    translateX = newWidth;
                    translateY = newHeight;
                } else if (rotation == 270) {
                    translateX = newWidth;
                }
                AffineTransform translateMatrix = AffineTransform.getTranslateInstance(translateX, translateY);
                // 先缩放，后旋转
                rotateMatrix.concatenate(scaleMatrix);
                // 先旋转，后平移
                translateMatrix.concatenate(rotateMatrix);
                canvas.concatMatrix(translateMatrix);
                PdfFormXObject pageCopy = origPage.copyAsFormXObject(pdfWriterDocument);
                canvas.addXObjectAt(pageCopy, 0, 0);
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

}
